package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.ProDto;
import com.example.dto.ProTypeDto;
import com.example.mapper.ProMapper;
import com.example.mapper.ProTypeMapper;
import com.example.service.ProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProServiceImpl implements ProService {
    @Autowired
    private ProMapper mapper;
    @Autowired
    private ProTypeMapper proTypeMapper;

    @Override
    public void add(ProDto dto) {
        mapper.insert(dto);
    }

    @Override
    public IPage<ProDto> select(ProDto dto) {
        QueryWrapper<ProDto> queryWrapper = new QueryWrapper<>();
        if (dto.getName()!=""&&dto.getName()!=null){
            queryWrapper.eq("name",dto.getName());
        }
        if (dto.getTypeId()!=null && 0 != dto.getTypeId()){
            queryWrapper.eq("type_id",dto.getTypeId());
        }
        if (dto.getState()!=null && !dto.getState().isEmpty()){
            queryWrapper.eq("state",dto.getState());
        }
        Page<ProDto> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<ProDto> result = mapper.selectPage(page,queryWrapper);
        List<ProDto> proList = result.getRecords();

        for (ProDto proDto:proList){
            proDto.setTypeName(proTypeMapper.selectById(proDto.getTypeId()).getCategoryName());
        }
        return result;
    }
    @Override
    public List<Map<String, Object>> selectName() {
        return mapper.selectName();
    }
    @Override
    public void update(ProDto dto) {
        QueryWrapper<ProDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        mapper.update(dto,queryWrapper);
    }

    @Override
    public void delete(ProDto dto) {
        QueryWrapper<ProDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        mapper.delete(queryWrapper);
    }
}
